package utilities;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class FileTools {

    public static String readTextFile(String fileFullPath, String ENCODING) throws IOException {
        String fileContent = "";
        FileInputStream fis = new FileInputStream(fileFullPath);
        InputStreamReader isr = new InputStreamReader(fis, ENCODING);
        BufferedReader reader = new BufferedReader(isr);

        String line = null;
        while ((line = reader.readLine()) != null) {
            fileContent += line + "\n";
        }

        reader.close();
        isr.close();
        fis.close();

        return fileContent;
    }

    public static List<String> readLinesTextFile(String fileFullPath, String ENCODING) throws IOException {
        ArrayList<String> lines = new ArrayList<String>();

        FileInputStream fis = new FileInputStream(fileFullPath);
        InputStreamReader isr = new InputStreamReader(fis, ENCODING);
        BufferedReader reader = new BufferedReader(isr);

        String line = null;
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }

        reader.close();
        isr.close();
        fis.close();

        return lines;
    }

    /*
     * Remplit foundFiles; liste des fichiers trouvés
     */
    public static void findFiles(String folderPath, ArrayList<File> foundFiles) {
        if (foundFiles == null) {
            foundFiles = new ArrayList<File>();
        }

        File currentForlderAsFile = new File(folderPath);

        //Le chemin en paramètre est un fichier
        if (!currentForlderAsFile.isDirectory()) {
            foundFiles.add(currentForlderAsFile);
            return;
        }

        File root = new File(folderPath);
        File[] list = root.listFiles();

        for (File f : list) {
            if (f.isDirectory()) {
                findFiles(f.getAbsolutePath(), foundFiles);
                //System.out.println("Dossier:" + f.getAbsoluteFile());
            } else {
                foundFiles.add(f);
                //System.out.println("Fichier:" + f.getAbsoluteFile());
            }
        }
    }
}
